﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SmallJob.Services.Implements;
using SmallJob.Services.Interfaces;
using SmallShop.Services.Interfaces;
using SmallShop.Services.Code;

namespace BackEnd.Controllers
{
    [Authorize]
    public class OrderController : Controller
    {
        //
        // GET: /Order/
        private readonly IUserService _UserService;
        private readonly IOrderService _OrderService;
        private readonly IProvideService _ProvideService;
        private readonly IRequestService _RequestService;
        private readonly IMMessageService _MessageService;
        private readonly IMConversationService _MConversationService;
        public OrderController(IMConversationService MConversationService,IUserService UserService, IOrderService OrderService, IRequestService RequestService, IProvideService ProvideService, IMMessageService MessageService)
        {
            this._UserService = UserService;
            this._OrderService = OrderService;
            this._ProvideService = ProvideService;
            this._RequestService = RequestService;
            _MessageService = MessageService;
            _MConversationService = MConversationService;
        }

        public ActionResult Index(string page, string filter, string filtertype, string filterstatus, string filterpayment)
        {
            if (string.IsNullOrWhiteSpace(page))
            {
                page = "1";
            }
            ViewData["tableindex"] = new { page = page, pagesize = Convert.ToInt32(Resources.Resource.ItemPerPage), filter = filter, filtertype = filtertype, filterstatus = filterstatus, filterpayment = filterpayment };
            return View();
        }
        public ActionResult TableOrder(int page, int pagesize, string filter, string filtertype, string filterstatus, string filterpayment)
        {

            ViewData["filter"] = filter;
            ViewData["filtertype"] = filtertype;
            ViewData["filterstatus"] = filterstatus;
            ViewData["filterpayment"] = filterpayment;
            int total = 0;
            pagesize = Convert.ToInt32(Resources.Resource.ItemPerPage);
            if (page == 0)
            {
                page = 1;
            }
            var model = _OrderService.GetByPageFilter(page, pagesize, out total, filter, filterstatus, filterpayment, filtertype);
            if ((total % pagesize) > 0)
            {
                ViewData["total"] = (total / pagesize) + 1;
            }
            else
            {
                ViewData["total"] = total / pagesize;
            }
            foreach (var item in model)
            {
                item.Buyer = _UserService.GetById(item.BuyerID);
                item.Seller = _UserService.GetById(item.SellerID);
                if (item.Type == (int)OrderType.PROVIDE)
                {
                    item.Provide = _ProvideService.GetById(item.ProvideID);
                }
                if (item.Type == (int)OrderType.REQUEST)
                {
                    item.Request = _RequestService.GetById(item.RequestID);
                }
            }
            ViewData["curpage"] = page;
            ViewData["nextpage"] = page + 1;
            ViewData["prepage"] = page - 1;
            return PartialView(model);
        }
        public ActionResult Details(int id,int userid)
        {
            var user = _UserService.GetById(userid);

            var cvsid = MessageHelper.BuildShortIDOrder(id);
            var order = _OrderService.GetById(id);
            if (order.DisableDetails || (user.ID != order.BuyerID && user.ID != order.SellerID)) return HttpNotFound("Cannot access.");

            if (order.Type == (int)OrderType.PROVIDE)
            {
                order.Provide = _ProvideService.GetById(order.ProvideID);
            }
            else
            {
                order.Request = _RequestService.GetById(order.RequestID);
            }
            var cvs = _MConversationService.GetConversationByShortID(cvsid);
            cvs.Messages = _MessageService.GetMessagesByShortID(cvs.ShortID);
            var model = cvs;
            ViewData["curuserid"] = user.ID;
            ViewData["displayorder"] = order;
            return View(model);
        }
        public ActionResult Complaint(string page, string filter)
        {
            if (string.IsNullOrWhiteSpace(page))
            {
                page = "1";
            }
            ViewData["tableindex"] = new { page = page, pagesize = Convert.ToInt32(Resources.Resource.ItemPerPage), filter = filter };
            return View();
        }
        public ActionResult TableComplaint(int page, int pagesize, string filter)
        {
            long total = 0;
            pagesize = Convert.ToInt32(Resources.Resource.ItemPerPage);
            if (page == 0)
            {
                page = 1;
            }
            var model = _MessageService.GetByPageComplaint(page, pagesize, out total);
            if (!string.IsNullOrWhiteSpace(filter))
            {
                model = _MessageService.GetByPageComplaintFilter(page, pagesize, out total, filter);
                ViewData["filter"] = filter;
            }
            if ((total % pagesize) > 0)
            {
                ViewData["total"] = (total / pagesize) + 1;
            }
            else
            {
                ViewData["total"] = total / pagesize;
            }
            ViewData["curpage"] = page;
            ViewData["nextpage"] = page + 1;
            ViewData["prepage"] = page - 1;
            return PartialView(model);
        }

        public ActionResult DetailComplaint(string id)
        {
            var mes = _MessageService.GetById(id);
            if (mes == null) return Json(false);
            ViewData["mes"] = mes;
            return PartialView();
        }
    }
}
